<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/20
 * Time: 下午5:44
 */

class ModRealTime {

    public static function getFiveMinuteData($platform,$game,$server,$type,$date,$special_id,$separate=false){
        $table = self::getRealTimeTable($type) . "_#Ym#";
        $ym = str_replace("-","",substr($date,0,7));
        $table = str_replace("#Ym#",$ym,$table);

        $where =  ' WHERE RecDate between :begin AND :end AND GameSrc=:game ';
        $start = strtotime($date);
        $end = $start + 86400 - 1;
        $args   = array(
            ':date'     => $date,
            //':platform' => $platform,
            ':period'   => "Daily",
            ':begin'    => $start,
            ':end'      => $end,
            ':game'     => $game,
        );

//         if(!empty($server) && $server !== 'ALL'){
//             $where .= 'AND SID=:server';
//             $args[':server'] = $server;
//         }else{
//         	if($separate == true && $type == 'Online'){
//         		$where .= ' AND SID != "ALL" ';
//         	}else{
//         		$where .= 'AND SID = "ALL"';
//         	}
//         }
        
        $user = AccAuth::me();
        if($platform != 'ALL' || $user['IsViewAll'] != 1){
        	$where .= " AND Platform=:Platform ";
        	$args[':Platform'] = $platform;
        }
         
        if($platform == 'ALL' && $server == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
        	$modServer = new ModServer();
        	$list = $modServer->getServerForSpecialId($game,$special_id);
        	$where .= " AND ( ";
        	foreach ($list as $key=>$val){
        		$where .= " SID = '{$val['develop_id']}'  or";
        	}
        	$where = substr($where, 0, -2);
        	$where .= " ) ";
        }else{
        	$where .= " AND SID=:ser ";
        	$args[':ser'] = empty($server) ? 'ALL' : $server;
        }

        $fields = self::getFiveMinuteSelect($type);
        $sql = "SELECT RecDate,Platform,GameSrc,SID,$fields
                FROM {$table} {$where} order by ( SUBSTRING(SID, 2) + 0 ) ";
        
//         var_dump($args);
//         echo $sql;exit;
        
        $dbLink = new PubDbLink('dbSelf', PubDatabase::getRtDbName($platform,$game));
        return $dbLink->query($sql, $args);
    }

    public static function getRealTimeTable($type){

        switch($type){
            case "PayUser" :
                $type = "Pay";
                break;
            case "PayMoney":
                $type = "Pay";
                break;
            case "PayCount":
                $type = "Pay";
                break;
            case "FirstPayUser" :
                $type = "Pay";
                break;
            case "FirstPaymoney":
                $type = "Pay";
                break;
            case "FirstPayCount":
                $type = "Pay";
                break;
            case "LoginUser":
                $type = "Login";
                break;
            case "RoleUser":
                $type = "Reg";
                break;
            case "RegUser":
                $type = "Reg";
                break;
            case "AccountUser":
                $type = "Reg";
                break;
            default:
                $type = $type;
        }
        return "tblRealTime_" . $type;
    }
    public static function getFiveMinuteSelect($type){
        $kpi = " '$type' as KPI,";
        if($type == "LoginUser"){
            return $kpi . " LoginUser as KPIValue,CumLoginUser as CumKPIValue ";
        }
        if($type == "PayUser"){
            return $kpi . " PayUser as KPIValue,CumPayUser as CumKPIValue";
        }
        if($type == "PayMoney"){
            return $kpi . " PayMoney as KPIValue,CumPayMoney CumKPIValue ";
        }
        if($type == "PayCount"){
        	return $kpi . " PayCount as KPIValue,CumPayCount as CumKPIValue";
        }
        if($type == "FirstPayUser"){
        	return $kpi . " FirstPayUser as CumKPIValue";
        }
        if($type == "FirstPaymoney"){
        	return $kpi . " FirstPaymoney CumKPIValue ";
        }
        if($type == "FirstPayCount"){
        	return $kpi . " FirstPayCount as CumKPIValue";
        }
        if($type == "Online"){
            return $kpi . " OnlineUser as KPIValue, OnlineUser as CumKPIValue ";
        }
        if($type == "RoleUser"){
            return $kpi . " RoleUser as KPIValue,CumRoleUser as CumKPIValue ";
        }
        if($type == "RegUser"){
        	return $kpi . " RegUser as KPIValue,CumRegUser as CumKPIValue ";
        }
        if($type == "AccountUser"){
            return $kpi . " AccountUser as KPIValue,CumAccountUser as CumKPIValue ";
        }
        return "";
    }

    public static function getHourData($platform,$game,$server,$type,$date, $separate=false){
        $table = self::getRealTimeTable($type) . "_Hrly";

        $game = strtoupper($game);
        $where =  ' WHERE RecDate = :date AND Platform = :platform AND GameSrc=:game ';

        $args   = array(
            ':date'     => $date,
            ':platform' => $platform,
            ':period'   => "Daily",
            ':date'     => $date,
            ':game'     => $game,
        );

        if(!empty($server) && $server !== 'ALL'){
            $where .= 'AND SID=:server';
            $args[':server'] = $server;
        }else{
            if($separate == true && $type == 'Online'){
                $where .= ' AND SID != "ALL" ';
            }else{
                $where .= 'AND SID = "ALL"';
            }
        }

        $fields = self::getHourSelect($type);
        $sql = "SELECT HR,Platform,GameSrc,SID,$fields
                FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getRtDbName($platform,$game));
        return $dbLink->query($sql, $args);
    }
    public static function getHourSelect($type){
        $kpi = " '$type' as KPI,";
        if($type == "LoginUser"){
            return $kpi . " LoginUser KPIValue, CumLoginUser as CumKPIValue ";
        }
        if($type == "PayUser"){
            return $kpi . " PayUser as KPIValue, CumPayUser as CumKPIValue ";
        }
        if($type == "PayMoney"){
            return $kpi . " PayMoney as KPIValue, CumPayMoney as CumKPIValue ";
        }
        if($type == "Online"){
            return $kpi . " OnlineUser as KPIValue, OnlineUser as CumKPIValue ";
        }
        if($type == "RoleUser"){
            return $kpi . " RegUser as KPIValue, CumRegUser as CumKPIValue ";
        }
        if($type == "AccountUser"){
            return $kpi . " AccountUser as KPIValue, CumAccountUser as CumKPIValue ";
        }
        return "";
    }
}